using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace CloudApiServer.Helper;
/// <summary>
/// http请求帮助类
/// </summary>
public class HttpHelper
{
	/// <summary>
	/// HttpClient实例
	/// </summary>
	private static readonly HttpClient client = new HttpClient();
	/// <summary>
	/// HttpResponseResult 
	/// </summary>
	public class HttpResponseResult
	{
		/// <summary>
		/// 状态码
		/// </summary>
		public int StatusCode { get; set; }
		/// <summary>
		/// 响应内容
		/// </summary>
		public string? ResponseBody { get; set; } 
	}
	/// <summary>
	/// 发送GET请求
	/// </summary>
	/// <param name="url">请求的URL</param>
	/// <param name="headers">自定义请求头</param>
	/// <returns>包含状态码和响应内容的 HttpResponseResult</returns>
	public static async Task<HttpResponseResult> GetAsync(string url, Dictionary<string, string> headers = null)
	{
        LogHelper.Info("---------------------------远程服务请求开始-----------------------------");
        LogHelper.Info($"请求方法: GET");
        LogHelper.Info($"请求地址: {url}");
        using (var request = new HttpRequestMessage(HttpMethod.Get, url))
		{
			if (headers != null)
			{
				foreach (var header in headers)
                {
                    LogHelper.Info($"请求头: {header.Key}={header.Value}");
                    request.Headers.Add(header.Key, header.Value);
				}
			}

			HttpResponseMessage response = await client.SendAsync(request);
			string responseBody = await response.Content.ReadAsStringAsync();
            LogHelper.Info($"响应状态码: {(int)response.StatusCode} {response.StatusCode}");
			// 记录响应头
            foreach (var header in response.Headers)
            {
                LogHelper.Info($"响应头: {header.Key}={string.Join(",", header.Value)}");
            }
            //LogHelper.Info($"响应体: {responseBody}");
            

            LogHelper.Info("---------------------------远程服务请求结束-----------------------------");
            return new HttpResponseResult
			{
				StatusCode = (int)response.StatusCode, // 状态码
				ResponseBody = responseBody // 响应内容
			};
		}
	}

	/// <summary>
	/// 发送POST请求
	/// </summary>
	/// <param name="url">请求的URL</param>
	/// <param name="content">请求体内容</param>
	/// <param name="headers">自定义请求头</param>
	/// <returns>包含状态码和响应内容的 HttpResponseResult</returns>
	public static async Task<HttpResponseResult> PostAsync(string url, string content, Dictionary<string, string> headers = null)
	{
        LogHelper.Info("---------------------------远程服务请求开始-----------------------------");
        LogHelper.Info($"请求方法: POST");
        LogHelper.Info($"请求地址: {url}");
        LogHelper.Info($"请求体: {content}");
        using (var request = new HttpRequestMessage(HttpMethod.Post, url))
		{
			if (headers != null)
			{
				foreach (var header in headers)
				{
                    LogHelper.Info($"请求头: {header.Key}={header.Value}");
                    request.Headers.Add(header.Key, header.Value);
				}
			}

			request.Content = new StringContent(content, Encoding.UTF8, "application/json");
			HttpResponseMessage response = await client.SendAsync(request);
			string responseBody = await response.Content.ReadAsStringAsync();

            LogHelper.Info($"响应状态码: {(int)response.StatusCode} {response.StatusCode}");
            // 记录响应头
            foreach (var header in response.Headers)
            {
                LogHelper.Info($"响应头: {header.Key}={string.Join(",", header.Value)}");
            }
            //LogHelper.Info($"响应体: {responseBody}");
            LogHelper.Info("---------------------------远程服务请求结束-----------------------------");
            return new HttpResponseResult
			{
				StatusCode = (int)response.StatusCode, 
				ResponseBody = responseBody 
			};
		}
	}
	
	/// <summary>
	/// 发送GET请求以获取图片
	/// </summary>
	/// <param name="url">图片接口的URL</param>
	/// <param name="headers">自定义请求头</param>
	/// <returns>图片的字节数组</returns>
	public static async Task<byte[]> GetImageAsync(string url, Dictionary<string, string> headers = null)
	{
		LogHelper.Info("---------------------------远程服务请求开始-----------------------------");
		LogHelper.Info($"请求方法: GET");
		LogHelper.Info($"请求地址: {url}");
		using (var request = new HttpRequestMessage(HttpMethod.Get, url))
		{
			if (headers != null)
			{
				foreach (var header in headers)
				{
					LogHelper.Info($"请求头: {header.Key}={header.Value}");
					request.Headers.Add(header.Key, header.Value);
				}
			}

			HttpResponseMessage response = await client.SendAsync(request);
			byte[] imageBytes = await response.Content.ReadAsByteArrayAsync();

			LogHelper.Info($"响应状态码: {(int)response.StatusCode} {response.StatusCode}");
			// 记录响应头
			foreach (var header in response.Headers)
			{
				LogHelper.Info($"响应头: {header.Key}={string.Join(",", header.Value)}");
			}
			LogHelper.Info("---------------------------远程服务请求结束-----------------------------");

			return imageBytes;
		}
	}
}